package be.djdb.custom;
import java.io.IOException;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jface.text.templates.persistence.TemplateStore;
import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import be.djdb.utils.Projectresources;

public class Aworkspace{
	private IWorkspace workspace;
    private IResource resource;
    private IContainer container = null;
    private IProject[] iprojects;
    private IProject Aproject;
    private Projectresources resources;
    private IProgressMonitor monitor;
    public Aworkspace() {
    	workspace = ResourcesPlugin.getWorkspace();
	}
    /**
     * Make a nature 
     * public static final String NATURE_ID = "domain.eclipse.nature";// nature (for .project)
     * 
     * */
    public Aworkspace(String containerName,IProgressMonitor monitor) {
    	workspace = ResourcesPlugin.getWorkspace();
    	IWorkspaceRoot root= workspace.getRoot();
    	iprojects = root.getProjects();
    	resource = root.findMember(new Path(containerName));
		if (resource.exists() && (resource instanceof IContainer)) {
			container =(IContainer) resource;
		}	
		monitor.beginTask(resources.getlblFor("eclipse.creatingproject"),50);
	}
    public IProject createnewproject(String projectName,IProgressMonitor monitor){
    	ANEWProject prj = new ANEWProject(ResourcesPlugin.getWorkspace());//.getRoot().getFullPath()
    	try {
			prj.create(projectName,this.monitor);
			return  prj.getproject();
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
    }
    
    public IPath getPath(IPath location){
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IWorkspaceRoot root = workspace.getRoot();
        IFile[] files = root.findFilesForLocation(location);
        IContainer[] folders =  root.findContainersForLocation(location);
        if(files.length > 0){
            for (int i = 0; i < files.length; i++)
                return files[i].getProjectRelativePath();
        }else{
            for (int i = 0; i < folders.length; i++)
                return folders[i].getProjectRelativePath();
        }
        return null;
    }
    class ANEWProject {
    	private IWorkspace iworks;
		private IProject NEWproject;
		protected int RemWU;
		/**
		 * @param root ResourcesPlugin.getWorkspace().getRoot();
		 */
    	public ANEWProject(IWorkspace workspace){
    		this.iworks = workspace;
		}
    	public ANEWProject(IProject reworkoldproject){
    		this.NEWproject = reworkoldproject;
		}
    	@SuppressWarnings("unused")
		private void create( String name) throws CoreException{
    		create(name,new NullProgressMonitor(),null);
    	}
    	@SuppressWarnings("unused")
    	private void create(String name,IPath workspacelocation) throws CoreException{
    		create(name,new NullProgressMonitor(),workspacelocation);
    	}
    	private void create(String name,IProgressMonitor monitor) throws CoreException{
    		create(name,monitor,null);
    	}
    	/**
    	 * @param name 				Name of the project
    	 * @param monitor 			The progressMonitor if it is used >>>>>>>>monitor = monitor != null ? monitor : new NullProgressMonitor();<<<<<<<<
    	 * @param workspacelocation The workspace when they is not the 
    	 * */
    	private void create(String name,IProgressMonitor monitor,IPath workspacelocation) throws CoreException{
    		monitor = monitor != null ? monitor : new NullProgressMonitor();
            NEWproject = iworks.getRoot().getProject(name);//ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
            IProjectDescription description = workspace.newProjectDescription(NEWproject.getName());
            if(workspacelocation != null && !Platform.getLocation().equals(workspacelocation)){
               description.setLocation(workspacelocation);
            }
    		try {
    			if (!NEWproject.exists()){RemWU--;
    				NEWproject.create(description, new SubProgressMonitor(monitor, 1));
    			}
    			if (!NEWproject.isOpen()){RemWU--;
    				NEWproject.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(monitor, 1));
    			}
    		} catch (CoreException e){
    		    System.out.println(e);
    			// TODO throw new Exception(e);
    		}
    	}
    	public IProject getproject(){
    		return NEWproject;
    	}
    	public void makenature(String[]natureid) throws CoreException{
    		IProjectDescription description=NEWproject.getDescription();
    		description.setNatureIds(natureid);
    		NEWproject.setDescription(description,null);
    	}
    }
}